﻿function HtmlEncode(value) {
    //create a in-memory div, set it's inner text(which jQuery automatically encodes)
    //then grab the encoded contents back out.  The div never exists on the page.
    return $('<div/>').text(value).html();
}

function HtmlDecode(value) {
    return $('<div/>').html(value).text();
}

$(document).ready(function () {
    $('[rel=tooltip]').tooltip(); 
});
function BindDDL(objects, ddl) {
    $(ddl).html('');
    if($(ddl).attr('multiple') != 'multiple')
        $(ddl).append($('<option value="0">Please Select</option>'));
    if (objects != null && objects.length > 0) {
        for (i = 0; i < objects.length; i++) {
            var type = objects[i];
            $(ddl).append($('<option></option>').attr("value", type.ID).text(type.Name));
        }
    }
}
function ShowLoadingAnimation(shoBlockingContainer, message) {
    message = typeof (message) == 'undefined' || message == null || message.length == 0 ? '' : message;
    $('#divLoadingAnimation div').html(message);
    $('#divLoadingAnimation').show();
    if (shoBlockingContainer) {        
        $('#divBlockingContainer').width($(document).width()).height($(document).height()).show();
        $(window).resize(function () {
            $('#divBlockingContainer').width($(document).width()).height($(document).height());
        });
    }
}
function HideLoadingAnimation() {
    $('#divLoadingAnimation').hide();
    $('#divBlockingContainer').hide();
}
function ShowCenteredPopUp(pageURL, windowName, width, height, isScrolling) {
    //alert(pageURL);
    var scrollbars = isScrolling ? 'yes' : 'no';
    var leftPosition = (screen.width) ? (screen.width - width) / 2 : 0;
    var topPosition = (screen.height) ? (screen.height - height) / 2 : 0;
    topPosition -= 30;
    var settings = 'height=' + height + ',width=' + width + ',top=' + topPosition + ',left=' + leftPosition + ',resizable=no,scrollbars=' + scrollbars + ',menubar=no,toolbar=no,status=yes,location=no,directories=no,addressbar=no'
    var page = pageURL;
    win = window.open(page, windowName, settings);
    if (win != null) {
        win.window.focus();
    }
    return win;
}
function AddToFavoriteResume(resumeID, btn) {
    if ($(btn).hasClass('disabled')) return;
    ShowLoadingAnimation(false);
    Ajax('/Ajax/AddToFavoriteResume', 'resumeID=' + resumeID, function (result) {
        HideLoadingAnimation();
        var status = parseInt(result);
        if (status == 1) {
            $(btn).addClass('disabled').html('<i class="icon-star"></i> Added to Favorite');
        }
    });
}
function AddToFavoriteJob(jobID, btn) {
    if ($(btn).hasClass('disabled')) return;
    ShowLoadingAnimation(false);
    Ajax('/Ajax/AddToFavoriteJob', 'JobID=' + jobID + '&url=' + window.location, function (result) {
        HideLoadingAnimation();
        var status = parseInt(result);
        if (status == -1)
            window.location = '/Account/Logon';
        else if (status == 1) {
            $(btn).addClass('disabled').html('<i class="icon-star"></i> Added to Favorite');
        }
    });
}
function SuccessMessageBox(message, heading) {
    if (typeof (heading) == 'undefined' || heading.length == 0)
        heading = 'Success!';
    return '<div class="alert alert-success">' +
                '<button type="button" class="close" data-dismiss="alert">&times;</button>' +
                '<h4>' + heading + '</h4>' +
                message +
            '</div>';
}
function WarningMessageBox(message, heading) {
    if (typeof (heading) == 'undefined' || heading.length == 0)
        heading = 'Warning!';
    return '<div class="alert alert-block">' +
                '<button type="button" class="close" data-dismiss="alert">&times;</button>' +
                '<h4>' + heading + '</h4>' +
                message +
            '</div>';
}
function Ajax(url, data, successCallback) {
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: function (result) {
            successCallback(result);
        },
        statusCode: {
            404: function () {
                alert('ERROR: Unable to complete the AJAX Request.\nRequested URL: ' + url + ' was not found.\nPlease contact with site administrator.');
            }
        }
    });
}
function AjaxGET(url, successCallback) {
    $.ajax({
        type: 'GET',
        url: url,
        success: function (result) {
            successCallback(result);
        },
        statusCode: {
            404: function () {
                alert('ERROR: Unable to complete the AJAX Request.\nRequested URL: ' + url + ' was not found.\nPlease contact with site administrator.');
            }
        }
    });
}